// zh_jypx/pages/teacher/t_list.js
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeIndex: 0,
    hidden: false
  },
  //退出登录
  onTuichu: function () {
    var username = wx.getStorageSync('user_msg').username
    console.log(username)
    wx.clearStorage()
    wx.navigateBack({
      delta: 1000
    })
    app.util.request({
      "url": "entry/wxapp/Teacher_retrun",
      "cachetime": "0",
      data: { username: username},
      success: res => {
        console.log(res)
        if (res.data.code == '200') {
          // 用户退出成功
          // console.log('用户成功退出')
          wx.showToast({
            title: '退出登录',
          })
          setTimeout(function () {
            // wx.clearStorage()
            wx.removeStorageSync('user_info')
            wx.setStorageSync('succssLogin', false)
            wx.reLaunch({
              url: '../rank/login_firset',
            })
          }, 1500)
        } else {
          wx.showToast({
            title: '退出失败',
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var teacher_msg = wx.getStorageSync('user_msg')
    console.log(teacher_msg)

    var teacher_id = teacher_msg.id
    console.log("老师id"+teacher_id)
    that.setData({
      teacher_id: teacher_id
    })

    // ——————————————课程级别 ——————————
    app.util.request({
      'url': 'entry/wxapp/CateList',
      'cachetime': '0',
      data:{
        teacher_id: teacher_id
      },
      success: function (res) {
        console.log("级别")
        console.log(res.data)
        if(res.data.code==200){
          that.setData({
            level: res.data.list,
            hidden: true
          })
          that.order()
        }
      },
    })

  },
  order: function (e) {
    var that = this;
    console.log(that)
    var first_id = that.data.level[0].id
    var activeIndex = that.data.activeIndex
    var teacher_msgs = wx.getStorageSync('user_msg')
    var teachers_id = teacher_msgs.id
    console.log(first_id)
    //——————————————课程 ——————————
    app.util.request({
      'url': 'entry/wxapp/CourseList',
      'cachetime': '0',
      data: {
        course_id: first_id,
        teacher_id: teachers_id
      },
      success: function (res) {
        console.log("级别详情")
        console.log(res.data)
        if(res.data.code==200){
          that.setData({
            con: res.data.list,
            none: res.data
          })
        }else{
          that.setData({
            con: res.data.list,
            none:res.data
          })          
        }

      },
    })
  },

  /*点击课程选择 */
  onDetail: function (e) {
    var that = this;
    console.log(e)
    console.log(that.data)
    var detail_id = e.currentTarget.dataset.id
    var activeIndex = that.data.activeIndex;
    var index = e.currentTarget.dataset.index;
    var teacher_msgs = wx.getStorageSync('teacher_msg')
    var teachers_id = teacher_msgs[0].id
    console.log(activeIndex)
    console.log(teacher_msgs)
    app.util.request({
      'url': 'entry/wxapp/CourseList',
      'cachetime': '0',
      data: {
        course_id: detail_id,
        teacher_id: teachers_id
      },
      success: function (res) {
        console.log("级别详情")
        console.log(res.data)

        if(res.data.code==200){
          that.setData({
            con: res.data.list,
          })
        }else{
          that.setData({
            none: res.data,
          }) 
        }

      },
    })
    that.setData({
      index: index,
      activeIndex: index
    })

  },

  /*点击进入班级 */
  t_class:function(e){
    var that=this;
    var cou_id = e.currentTarget.dataset.id
    var teacher_id = that.data.teacher_id
    console.log(teacher_id)
    wx:wx.navigateTo({
      url: 't_classes?cou_id=' + cou_id + "&teacher_id=" + teacher_id,
    })
    // var u_id = wx.getStorageSync('user_msg').id
    // console.log(u_id)
    // app.util.request({
    //   url: "entry/wxapp/IsLogin",
    //   'cachetime': '0',
    //   data: {
    //     u_id: u_id
    //   },
    //   success: function (res) {
    //     console.log(res)
    //     if (res.data.code == 501) {
    //       wx.showModal({
    //         title: '提示',
    //         content: '该用户已被注销,请重新登录!',
    //         success: function (res) {
    //           if (res.confirm) {
    //             console.log('用户点击确定')
    //             wx: wx.navigateTo({
    //               url: '../rank/login_firset',
    //             })
    //           } else if (res.cancel) {
    //             console.log('用户点击取消')
    //           }
    //         }
    //       })
    //     } else {
    //       wx: wx.navigateTo({
    //         url: 't_classes?cou_id=' + cou_id + "&teacher_id=" + teacher_id,
    //       })
    //     }
    //   }
    // })
  },

  //点击扫码
  onCan:function(e){
    wx.scanCode({
      success: (res) => {
        console.log(res)
        //var path = "zh_jypx/pages/teacher/btn?scene=2,20"
        var path = res.path
        console.log("路径")
        console.log(path)
        var arr = path.slice(32)
        console.log(arr)
        wx.navigateTo({
          url: 'btn?arr=' + arr,
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})